✸ ✸ ✸
1.fetch方法:
App.vue
export default {
name: 'App',
data() {
return {}
},
//fetch
created() {
fetch("/apis/test/testToken.php", {
method:"post",
headers:{
token:"f4c9023jkwalkfjas2910a"
},
body:JSON.stringify(
{username:"brownwang",password:"21232"}
)
.then(result =>{
return result.json()
})
.then(data => {
console.log(data)
})
})
}
}
/config/index.js
proxyTable: {
'/apis':{
target:'https://goods.footer.com', //接口名称
changeOrigin:true, //是否跨域
pathRewrite:{
'^/apis':'' //需要rewrite重写的
}
}
},
2.axios
npm install axios
配置main.js
import axios from 'axios'
axios.defaults.headers.common['token']='12fdafsafdfsadfsaf'
axios.defaults.headers.post["Content-type"]="application/json"
Vue.prototype.$axios=axios
App.vue实现:
export default {
name: 'App',
data() {
return {}
},
//axios
created() {
this.$axios.post("/apis/test/testToken.php",
{username:"brownwang",password:"1232"})
.then(data=>{
console.log(data)
})
}
}
✸ ✸ ✸
📜 版权声明
本文作者:王梓 | 原文链接:https://www.bthlt.com/note/7681731-CssJsHtmlvue.js 跨域请求
出处:葫芦的运维日志 | 转载请注明出处并保留原文链接


📜 留言板
留言提交后需管理员审核通过才会显示